package demo1;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-09-07
 * Time: 9:28
 */
public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        int[] arr1 = {2,1,0};
        int[] arr2 = {1,2,0};
        System.out.println(test.IsPopOrder(arr1,arr2));

    }
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(!stack.isEmpty() && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        if(!stack.isEmpty()) {
            return false;
        }
        return true;
    }
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if(c == '[' || c == '(' || c == '{') {
                stack.push(c);
            }else {
                if(stack.isEmpty()) {
                    return false;
                }
                if(stack.peek() == '(' && c == ')' || stack.peek() == '['
                        && c == ']' || stack.peek() == '{' && c == '}') {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(stack.isEmpty()) {
            return true;
        }
        return false;
    }

    public static void main2(String[] args) {
        Test test = new Test();
        String s = "()";
        System.out.println(test.isValid(s));
    }
    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.isEmpty();
    }
}
